"use strict";

$(function () {

    let oldPassword = $("#mh-oldpassword");
    let newPassword = $("#mh-newpassword");
    let rNewPassword = $("#mh-rnewpassword");

    let groupOldPassword = $("#mh-group-oldpassword");
    let groupNewPassword = $("#mh-group-newpassword");
    let groupRNewPassword = $("#mh-group-rnewpassword");

    let saveBtn = $("#mh-save");

    groupOldPassword.removeClass("has-success", "has-error")
    groupNewPassword.removeClass("has-success", "has-error")
    groupRNewPassword.removeClass("has-success", "has-error")

    oldPassword.on("blur", function () {
        $.ajax({
            type: "GET",
            url: path + "/user",
            data: {method: "pwdModify", oldPassword: oldPassword.val()},
            dataType: "json",
            success: function (data) { // 请求没问题
                if (data.status == "true") {
                    groupOldPassword.addClass("has-success");
                } else if (data.status == "false") { // 与原密码不符合
                    groupOldPassword.addClass("has-error")

                } else if (data.status == "sessionError") { // session过期
                    groupOldPassword.addClass("has-error")
                    alert("登陆时间过长，请重新登陆")
                } else if (data.status == "error") { // 输入为空
                    groupOldPassword.addClass("has-error")
                }
            },
            error: function (data) {
                groupOldPassword.addClass("has-error")
                alert("请求出错！")
            }
        });

    }).on("focus", function () {
        groupOldPassword.removeClass("has-success").removeClass("has-error")
    });

    newPassword.on("blur", function () {
        if (oldPassword.val() != newPassword.val() && newPassword.val() != null && newPassword.val() != "") {
            groupNewPassword.addClass("has-success")
        } else {
            groupNewPassword.addClass("has-error")
        }
    }).on("focus", function () {
        groupNewPassword.removeClass("has-success").removeClass("has-error")
    })

    rNewPassword.on("blur", function () {
        if (rNewPassword.val() != null && rNewPassword.val() == newPassword.val() && rNewPassword.val() != null && rNewPassword.val() != "") {
            groupRNewPassword.addClass("has-success")
        } else {
            groupRNewPassword.addClass("has-error")
        }
    }).on("focus", function () {
        groupRNewPassword.removeClass("has-error").removeClass("has-success")
    })


})